/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wf3.core.manager;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import wf3.core.dao.DAO;
import wf3.core.dao.GeovolutionDAO;
import wf3.core.dao.LoginDAO;
import wf3.core.dao.PageDAO;
import wf3.core.dao.WorkflowDAO;

/**
 *
 * @author Rómulo Galindo Tanta
 */
public class DAOFactory extends Factory {

    private static LinkedBlockingQueue<DAO> lsPrincipal;
    private static boolean run = false;

    static {
        lsPrincipal = new LinkedBlockingQueue<>();
    }

    public DAOFactory() {
    }

    public static synchronized DAO get_logindao() {
        if (run) {
            LoginDAO dao = new LoginDAO();
            try {
                lsPrincipal.put(dao);
            } catch (InterruptedException ep) {
                System.out.println("Error insertando dao:" + ep.getMessage());
                ep.printStackTrace();
            }
            return dao;
        } else {
            return null;
        }
    }

    public static synchronized DAO get_workflowdao(int id_sesion) {
        if (run) {
            WorkflowDAO dao = new WorkflowDAO(id_sesion);
            try {
                lsPrincipal.put(dao);
            } catch (InterruptedException ep) {
                System.out.println("Error insertando dao:" + ep.getMessage());
                ep.printStackTrace();
            }
            return dao;
        } else {
            return null;
        }
    }

    public static synchronized DAO get_workflowgdao(int id_sesion) {
        if (run) {
            GeovolutionDAO dao = new GeovolutionDAO(id_sesion);
            try {
                lsPrincipal.put(dao);
            } catch (InterruptedException ep) {
                System.out.println("Error insertando dao:" + ep.getMessage());
                ep.printStackTrace();
            }
            return dao;
        } else {
            return null;
        }
    }

    public static synchronized DAO get_pagedao(int id_sesion) {
        if (run) {
            PageDAO dao = new PageDAO(id_sesion);
            try {
                lsPrincipal.put(dao);
            } catch (InterruptedException ep) {
                System.out.println("Error insertando dao:" + ep.getMessage());
                ep.printStackTrace();
            }
            return dao;
        } else {
            return null;
        }
    }

    public static void closedao(DAO dao) {
        if (dao != null) {
            try {
                dao.CLOSE_SESSION();
            } catch (Exception ep) {
                System.out.println("closedao :" + ep.getMessage());
                ep.printStackTrace();
            }
        }
    }

    public static void garbage() {
        long mtime = System.currentTimeMillis();
//        System.out.println("========>>"+lsPrincipal.size()+"<===============");
        for (int i = 0; i < 100; i++) {
            try {
                DAO dao = lsPrincipal.poll(1, TimeUnit.SECONDS);
                if (dao != null) {
                    if (!dao.isClosed()) {
                        dao = null;
//                        System.out.println("Supuestamente esta cerrado y ya lo consumi");
                    } else if ((mtime - dao.getTimelife()) >= 100 * 30) {
                        try {
                            dao.CLOSE_SESSION();
                            dao = null;
                        } catch (Exception ep) {
                        }
//                        System.out.println("Supuestamente lo he cerrado y lo he consumido");
                    } else {
//                        System.out.println("Se devuelve al pool el elemento hasta que sea hora de destruirlo");
                        lsPrincipal.put(dao);
                    }
                } else {
//                    System.out.println("No hay elmento rompiendo");
                    break;
                }

            } catch (InterruptedException ep) {
                System.out.println("Error en garbage:" + ep.getMessage());
                ep.printStackTrace();
            }
        }
    }

    public static synchronized List<DAO> getdaos() {
        return null;
    }

    public static void stop() {
        run = false;
    }

    public static void start() {
        run = true;
    }

    public static void clean() {
        lsPrincipal.clear();
    }

    public static void clean_and_destoid() {
        for (DAO dao : lsPrincipal) {
            try {
                dao.CLOSE_SESSION();
            } catch (Exception ep) {
            }
        }
        lsPrincipal.clear();
    }

}
